package teach.aistar.controller;

import net.bytebuddy.implementation.ToStringMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import teach.aistar.model.entity.*;
import teach.aistar.model.vo.AdminCarQueryVo;
import teach.aistar.model.vo.Result;
import teach.aistar.service.*;


import teach.aistar.util.DateUtil;
import teach.aistar.util.FileUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class AdminCarController {
    @Autowired
    private ICarDetailService iCarDetailService;

    @Autowired

    private ICarBandsService iCarBandsService;


    @Autowired
    private ICarStyleService iCarStyleService;


    //首页
    @GetMapping("/index1")
    public String index() {
        return "jsp/backend/index";
    }


    //车辆信息展示
    @GetMapping("/list")
    public String list(HttpServletRequest request, HttpSession session) {
        List<CarDetail> lists = iCarDetailService.selectAll();
        List<CarBrands> carBrands=iCarBandsService.selectAll();
        List<CarStyle> carStyles=iCarStyleService.findAll();


        request.setAttribute("carDetails", lists);
        request.setAttribute("carBrands", carBrands);
        request.setAttribute("carStyles", carStyles);
        return "jsp/backend/page-list-product";
    }

    //车辆信息添加1
    @GetMapping("/addCarPage")
    public String bands(HttpServletRequest request, HttpSession session) {
        List<CarBrands> bands = iCarBandsService.selectAll();
        List<CarStyle> styles = iCarStyleService.findAll();
        request.setAttribute("brandsList", bands);
        request.setAttribute("styleList", styles);
        return "jsp/backend/page-add-product";
    }

    //车辆信息添加
    @PostMapping("/addCar")
    @ResponseBody
    public Result add(CarDetail carDetail,HttpServletRequest request, HttpSession session, HttpServletResponse response,@RequestParam(name = "file") MultipartFile file) throws ParseException {


//        System.out.println(file.getOriginalFilename());
//        System.out.println(carDetail);
        carDetail.setcPhoto(FileUtil.FileUpload("C:\\nginx\\nginx-1.19.10\\nginx-1.19.10\\html\\car",file).getImage());
//        String carBrands = (String) request.getParameter("carBrands");
//        String carstyle = (String) request.getParameter("carstyle");
//        String price = (String) request.getParameter("price");
//        String seatnum = (String) request.getParameter("seatnum");
//        String mile = (String) request.getParameter("mile");
//        String speed = (String) request.getParameter("speed");
//        String time = (String) request.getParameter("time");
//        String startime = (String) request.getParameter("startime");
//        String carPicture = (String) request.getParameter("carPicture");
//        String descript = (String) request.getParameter("descript");
//
//        CarDetail carDetail = new CarDetail();
//        if (carBrands != null) {
//            carDetail.setBrId(Long.valueOf(carBrands));
//        }
//        if (carstyle != null) {
//            carDetail.setStId(Long.valueOf(carstyle));
//        }
//        if (price != null) {
//            carDetail.setcOprice(Double.valueOf(price));
//        }
//        if (seatnum != null) {
//            carDetail.setcSeat(Integer.valueOf(seatnum));
//        }
//        if (mile != null) {
//            carDetail.setcMileage(Integer.valueOf(mile));
//        }
//        if (speed != null) {
//            carDetail.setcGear(speed);
//        }
//        if (time != null) {
//            carDetail.setcInsure(DateUtil.stringToDate(time));
//        }
//        if (startime != null) {
//            carDetail.setcRegistration(DateUtil.stringToDate(startime));
//        }
//        //处理车的图片路径
//        if (!carPicture.equals("")) {
//            String[] carPictures = carPicture.split("/");
//            carDetail.setcPhoto(carPictures[carPictures.length - 1]);
//        }

        //车辆图片上传

//
//        if (!descript.equals("")) {
//            carDetail.setcDesc(descript);
//        }
       int flag =  iCarDetailService.insert(carDetail);
        if(flag >0){
            return new Result("200","插入成功",null);
        }
        return new Result("500","插入失败",null);

    }


    //删除车辆信息
    @GetMapping("/deleteCar")
    @ResponseBody
    public Result deleteCar(HttpServletRequest request, HttpSession session) {
        Long  id=Long.valueOf(request.getParameter("id"));
//        System.out.println(id+"aaaaaaaaaaa");
        iCarDetailService.deleteById(id);
        return new Result("200");
    }

    //多条件查询
    @GetMapping("/selects")
    public String selects(HttpServletRequest request, HttpSession session) throws ParseException {
        //车辆信息多条件查询
        //车品牌（车型）
        String CreateDate=request.getParameter("CreateDate");
        String minPrice = request.getParameter("minPrice");
        String maxPrice=request.getParameter("maxPrice");
        String brands=request.getParameter("brands");


        AdminCarQueryVo adminCarQueryVo=new AdminCarQueryVo();



        if(minPrice!=null && minPrice.trim().length()>0){
            adminCarQueryVo.setMinPrice(Double.valueOf(minPrice));
        }

        if(maxPrice!=null && maxPrice.trim().length()>0){
            adminCarQueryVo.setMaxPrice(Double.valueOf(maxPrice));
        }

        if(brands.equals("-1")){
            brands = null;
        }
        if(brands!=null && brands.trim().length()>0){
            adminCarQueryVo.setBrandsId(Long.valueOf(brands));
        }

        if(CreateDate!=null && CreateDate.trim().length()>0) {
            Date date = DateUtil.stringToDate(CreateDate);
            adminCarQueryVo.setcCreateTime(date);
        }
        List<CarDetail> carDetailList=iCarDetailService.findByConditional(adminCarQueryVo);
        //查询条件回显页面
        request.setAttribute("ACreateDate",CreateDate);
        request.setAttribute("AminPrice",minPrice);
        request.setAttribute("AmaxPrice",maxPrice);
        request.setAttribute("Abrands",brands);


        //查到之后数据在前端显示
        List<CarBrands> carBrands=iCarBandsService.selectAll();
        List<CarStyle> carStyles=iCarStyleService.findAll();

        request.setAttribute("carDetails", carDetailList);

        request.setAttribute("carBrands", carBrands);
        request.setAttribute("carStyles", carStyles);
        return "jsp/backend/page-list-product";
    }

    //编辑车辆信息
    @GetMapping("/edit")
    public String edit(HttpServletRequest request) throws ParseException{

       long id=Long.parseLong(request.getParameter("id"));

        CarDetail carDetail=iCarDetailService.findById(id);
        request.setAttribute("carDetails",carDetail);

        List<CarBrands> brands=iCarBandsService.selectAll();
        List<CarStyle> carStyles=iCarStyleService.findAll();
        request.setAttribute("brandsList",brands);
        request.setAttribute("carStylesList",carStyles);
        request.setAttribute("id",id);


        return "jsp/backend/page-edit-product";
    }

    //编辑车辆信息2
    @GetMapping("/edit2")
    public String edit2(HttpServletRequest request) throws ParseException{
        Long id=Long.valueOf(request.getParameter("id"));
        String carBrands = (String) request.getParameter("carBrands");
        String carstyle = (String) request.getParameter("carstyle");
        String price = (String) request.getParameter("price");
        String seatnum = (String) request.getParameter("seatnum");
        String mile = (String) request.getParameter("mile");
        String speed = (String) request.getParameter("speed");
        String time = (String) request.getParameter("time");
        String startime = (String) request.getParameter("startime");
        String descript = (String) request.getParameter("descript");

        CarDetail carDetail=new CarDetail();
        carDetail.setId(id);
        if (carBrands != null) {
            carDetail.setBrId(Long.valueOf(carBrands));
        }
        if (carstyle != null) {
            carDetail.setStId(Long.valueOf(carstyle));
        }
        if (price != null) {
            carDetail.setcOprice(Double.valueOf(price));
        }
        if (seatnum != null) {
            carDetail.setcSeat(Integer.valueOf(seatnum));
        }
        if (mile != null) {
            carDetail.setcMileage(Integer.valueOf(mile));
        }
        if (speed != null) {
            carDetail.setcGear(speed);
        }
        if (time != null) {
            carDetail.setcInsure(DateUtil.stringToDate(time));
        }
        if (startime != null) {
            carDetail.setcRegistration(DateUtil.stringToDate(startime));
        }

        if (!descript.equals("")) {
            carDetail.setcDesc(descript);
        }
        System.out.println(carDetail);
        iCarDetailService.update(carDetail);

        return "redirect:/user/list";
    }



    //返回统计图标页面
    @GetMapping("/report")
    public String report() {

        return "jsp/backend/page-report";
    }



//    @GetMapping("/invoice")
//    public String test1() {
//        return "jsp/backend/auth-sign-in";
//    }

    //售后服务页面
    @GetMapping("/service")
    public String after() {
        return "jsp/home/after-sales";
    }
}
